﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using DeVetma.admin.classes;
using DeVetma.admin.models.database;
using DeVetma.admin.models.managers;
using DeVetma.admin.models.database.metadata;

namespace DeVetma.admin.models.entities
{
    [Entity(table = "Settings")]
    public class Setting
    {
        [EntityField(column = "id", keyField = true, nullValue = null)]
        public string id { get; set; }

        [EntityField(column = "type", keyField = false, nullValue = null)]
        public string type { get; set; }

        [EntityField(column = "name", keyField = false, nullValue = null)]
        public string name { get; set; }

        [EntityField(column = "value", keyField = false, nullValue = null)]
        public string _value { get; set; }

        public T value<T>()
        {
            MethodInfo pMethod = typeof(T).GetMethod("Parse", new Type[] { typeof(string) });

            if (pMethod != null)
                return (T)pMethod.Invoke(null, new object[] { _value });
            else if (typeof(T).Name == typeof(string).Name)
                return (T)((object)_value);
            

            throw new InvalidCastException("The value type is not primitive type");
        }

        public void value<T>(T value)
        {
            _value = value.ToString();
        }

        public override bool Equals(object obj)
        {
            return (obj is Setting && ((Setting)obj).name == name &&
                ((Setting)obj).type == type);
        }
    }
}